﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace rmTest.Common
{
    class Class1
    {
        /// <summary>
        /// 判断文件是否存在
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static bool FileIsExist(string uri)
        {
            if (uri.StartsWith("http://") || uri.StartsWith("https://"))//网络路径
            {
                HttpWebRequest req = null;
                HttpWebResponse res = null;
                try
                {
                    req = (HttpWebRequest)WebRequest.Create(uri);
                    req.Method = "HEAD";
                    req.Timeout = 100;
                    res = (HttpWebResponse)req.GetResponse();
                    return (res.StatusCode == HttpStatusCode.OK);
                }
                catch
                {
                    return false;
                }
                finally
                {
                    if (res != null)
                    {
                        res.Close();
                        res = null;
                    }
                    if (req != null)
                    {
                        req.Abort();
                        req = null;
                    }
                }
            }
            else//物理路径
            {
                return File.Exists(uri);
            }
        }

        /// <summary>
        /// 后台请求一般处理程序
        /// </summary>
        /// <param name="apiUrl"></param>
        /// <param name="queryString"></param>
        /// <returns></returns>
        public static string GetResponseByPost(string apiUrl, string queryString = null)
        {
            queryString = queryString + string.Empty;
            string responseString = string.Empty;
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(apiUrl + queryString);
            request.ContentType = "text/html";
            request.Method = "POST";
            request.ContentLength = queryString.Length;
            request.Timeout = 60000;
            byte[] bytes = Encoding.UTF8.GetBytes(queryString);
            Stream os = null;
            try
            { // send the Post
                request.ContentLength = bytes.Length;   //Count bytes to send
                os = request.GetRequestStream();
                os.Write(bytes, 0, bytes.Length);         //Send it
            }
            catch (WebException ex)
            {
                throw ex;
            }
            finally
            {
                if (os != null)
                {
                    os.Close();
                }
            }

            HttpWebResponse response = null;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    responseString = reader.ReadToEnd();
                }
            }
            catch (Exception ex2)
            {
                throw ex2;
            }
            finally
            {
                if (response != null)
                    response.Close();
            }
            return responseString;
        }
    }
}
